package com.neweagle.api.module.sys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import com.neweagle.api.comm.exception.UserExistException;
import com.neweagle.api.comm.plugin.security.model.AuthUser;
import com.neweagle.api.comm.utils.WebHelper;
import com.neweagle.api.comm.utils.WebUtils;
import com.neweagle.api.module.sys.entity.AppUser;
import com.neweagle.api.module.sys.mapper.AppUserMapper;
import com.neweagle.api.module.sys.service.IAppUserService;
import org.omg.CORBA.UserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * AppUser 表数据服务层接口实现类
 */
@Service
public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> implements IAppUserService {
    /**
     * 密码加密
     */
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public boolean deleteAll() {
        return retBool(baseMapper.deleteAll());
    }

    @Override
    public List<AppUser> selectListBySQL() {
        return baseMapper.selectListBySQL();
    }

    /**
     * @param mobile 手机号
     * @param verifyCode 验证码
     * @param password 密码
     * @return 注册
     * @throws Exception
     */
    @Override
    public boolean register(String mobile, String verifyCode, String password) throws Exception {
        AppUser user = new AppUser();
        user.setUsername(mobile);
        if (baseMapper.selectOne(user) != null) {
            throw new UserExistException();
        }
        user.setMobile(mobile);
        user.setNickname(mobile);
        user.setPassword(passwordEncoder.encode(password));
        return retBool(baseMapper.insert(user));
    }


    /**
     * @param mobile 手机号
     * @param password 密码
     * @return 登录
     * @throws Exception
     */
    @Override
    public boolean login(String mobile, String password) throws Exception {
        AppUser user = new AppUser();
        user.setUsername(mobile);
        if (baseMapper.selectOne(user) != null) {
            return true;
        }
        return false;
    }

    /**
     * @param mobile 手机号
     * @param password 密码
     * @param verifyCode 验证码
     * @return 忘记密码
     * @throws Exception
     */
    @Override
    public boolean fogetPassword(String mobile, String password, String verifyCode) throws Exception {
        AppUser user = new AppUser();
        user.setUsername(mobile);
        AppUser findUser = baseMapper.selectOne(user);
        if (null != findUser) {
            baseMapper.updateById(findUser);
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param mobile 手机号
     * @param password 密码
     * @param verifyCode 验证码
     * @return 修改密码
     * @throws Exception
     */
    @Override
    public boolean modifyPassword(String mobile, String password, String verifyCode) throws Exception {
        AppUser user = new AppUser();
        user.setUsername(mobile);
        user.setPassword(passwordEncoder.encode(password));
        AuthUser currentUser = WebUtils.getCurrentUser();
        user.setId(currentUser.getId());
        if (0 != baseMapper.updateById(user)) {
            return true;
        }
        return false;
    }

}